package com.education.platform.management.service.impl;

import com.education.platform.common.enums.FlowBusinessTypeEnum;
import com.education.platform.common.enums.FlowTypeEnum;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.service.impl.BaseServiceImpl;
import com.education.platform.dao.AccountDao;
import com.education.platform.dao.AccountFlowDao;
import com.education.platform.dao.UserBaseDao;
import com.education.platform.dao.entity.AccountDO;
import com.education.platform.dao.entity.AccountFlowDO;
import com.education.platform.management.config.ThreadUser;
import com.education.platform.management.enums.PushTypeEnum;
import com.education.platform.management.push.PushUtil;
import com.education.platform.management.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
* describe: 账户
* @author : tomato
* @date: 2020-06-29 10:28:06.251
**/
@Service
public class AccountServiceImpl extends BaseServiceImpl<AccountDao, AccountDO> implements AccountService {

	private static final Logger LOGGER = LoggerFactory.getLogger(AccountServiceImpl.class);

    @Autowired
	private AccountDao accountDao;

    @Autowired
    private UserBaseDao userBaseDao;
    @Autowired
    private AccountFlowDao accountFlowDao;
    @Autowired
    private PushUtil pushUtil;

    /**
     * 后台送豆子
     *
     * @param userId
     * @param beans
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void chargeBeans(Integer userId, Integer beans) {
        if(beans == null||beans<1){
            throw new BusinessException("赠送历史豆至少1个");
        }
        if(userId == null){
            throw new BusinessException("用户id不能为空");
        }
        AccountDO accountDO = accountDao.getByUserId(userId);
        if(accountDO == null){
            throw new BusinessException("用户账户不存在");
        }
        //加历史豆
        int n = accountDao.recharge(beans,userId);
        if(n!=1){
            throw new BusinessException("操作失败");
        }
        //保存记录
        this.saveAccountFlow(accountDO.getBeanNumber(),beans,userId);
        //推送消息
//        PushUtil pushUtil = PushUtil.getInstance();
        pushUtil.push(userId, PushTypeEnum.GIVE_BEANS,String.valueOf(beans),null);
    }

    /**
     * 保存记录
     * @param before
     * @param flow
     * @param userId
     */
    private void saveAccountFlow(Integer before,Integer flow,Integer userId){
        AccountFlowDO af = new AccountFlowDO();
        af.setUserId(userId);
        af.setBeforeNumber(before);
        af.setFlowNumber(flow);
        af.setAfterNumber(before+flow);
        af.setFlowType(FlowTypeEnum.IN.getValue());
        af.setBusinessType(FlowBusinessTypeEnum.GIVE_BEANS.getValue());
        af.setRemarks(FlowBusinessTypeEnum.GIVE_BEANS.getDes());
        af.setDeleted(false);
        af.setCreateTime(new Date());
        af.setCreateBy(ThreadUser.getUserName());
        accountFlowDao.insert(af);
    }
}
